/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: RatioImage.java
 * 		Description	: Ratio Utils Image
 * 		Called		: All
 * ================================================================================
 * */
package com.kompas.bola.application.ui;

import com.kompas.bola.application.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RatioImage extends ImageView {
	private static final int MEASUREMENT_WIDTH 					= 0;
	private static final int MEASUREMENT_HEIGHT 				= 1;
	private static final float DEFAULT_ASPECT_RATIO 			= 1.35f;
	
	private static final boolean DEFAULT_ASPECT_RATIO_ENABLED 	= true;
	private static final int DEFAULT_DOMINANT_MEASUREMENT 		= MEASUREMENT_WIDTH;

	private float aspectRatio;
	private boolean aspectRatioEnabled;
	private int dominantMeasurement;
	/** =========================================================================== */
	public RatioImage(Context context) {
		this(context, null);
	}
	/** =========================================================================== */
	public RatioImage(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a 											= context.obtainStyledAttributes(attrs,
																R.styleable.RatioImage);
		
		aspectRatio 											= a.getFloat(R.styleable.RatioImage_aspectRatio,
																DEFAULT_ASPECT_RATIO);
		aspectRatioEnabled 										= a.getBoolean(
																R.styleable.RatioImage_aspectRatioEnabled,
																DEFAULT_ASPECT_RATIO_ENABLED);
		
		dominantMeasurement 									= a.getInt(
																R.styleable.RatioImage_dominantMeasurement,
																DEFAULT_DOMINANT_MEASUREMENT);
		a.recycle();
	}
	/** =========================================================================== */
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		if (!aspectRatioEnabled)
			return;

		int newWidth 											= MeasureSpec.getSize(widthMeasureSpec);;
		int newHeight 											= 0;		
		
		switch (dominantMeasurement) {
		case MEASUREMENT_WIDTH:
			newWidth 											= getMeasuredWidth();
			newHeight 											= (int) (newWidth * aspectRatio)/2;
			break;

		case MEASUREMENT_HEIGHT:
			newHeight 											= getMeasuredHeight();
			newWidth 											= (int) (newHeight * aspectRatio);
			break;

		default:
			throw new IllegalStateException("Unknown measurement with ID "
					+ dominantMeasurement);
		}
		setMeasuredDimension(newWidth, newHeight);
	}
	/** =========================================================================== */
	public float getAspectRatio() {
		return aspectRatio;
	}
	/** =========================================================================== */
	public void setAspectRatio(float aspectRatio) {
		this.aspectRatio 										= aspectRatio;
		if (aspectRatioEnabled) {
			requestLayout();
		}
	}
	/** =========================================================================== */
	public boolean getAspectRatioEnabled() {
		return aspectRatioEnabled;
	}
	/** =========================================================================== */
	public void setAspectRatioEnabled(boolean aspectRatioEnabled) {
		this.aspectRatioEnabled 								= aspectRatioEnabled;
		requestLayout();
	}
	/** =========================================================================== */
	public int getDominantMeasurement() {
		return dominantMeasurement;
	}
	/** =========================================================================== */
	public void setDominantMeasurement(int dominantMeasurement) {
		if (dominantMeasurement != MEASUREMENT_HEIGHT
				&& dominantMeasurement != MEASUREMENT_WIDTH) {
			throw new IllegalArgumentException("Invalid measurement type.");
		}
		this.dominantMeasurement 								= dominantMeasurement;
		requestLayout();
	}
	/** =========================================================================== */
}
